package it.rikesh.aip2011.client.component;

import it.rikesh.aip2011.client.BaseLayout;
import it.rikesh.aip2011.client.service.HutService;
import it.rikesh.aip2011.client.service.HutServiceAsync;
import it.rikesh.aip2011.shared.model.Activity;
import it.rikesh.aip2011.shared.model.Trail;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

/**
 * @author  sunita
 */
public class HutLayout extends LayoutContainer {
    private static HutServiceAsync hutService = GWT.create(HutService.class);
    
    private Text txtName;
    private Text txtTitle;
    private Text txtPosition;
    private Text txtDescription;
    private Text lblPosGeo;
    private Text lblDescription;
    private Text lblActivity;
    private Text lblTrail;
    private List<Activity> activities;
    private List<Trail> trails;
    private HTML image;
    private FlexTable tableAct;
    private FlexTable tableTr;
    private String id;
    
    public HutLayout(String name,boolean link){
        hutService.getHutId(name, new AsyncCallback<String>() {
            public void onSuccess(String result) {
                id=result;
                init();
                getInfoHut(id);
                addComponent();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
    }
    
    public HutLayout(String id){
        init();
        getInfoHut(id);
        
        addComponent();
    }
    
    /**
     * Questo metodo si occupa di inizializzare tutte le variabili necessari
     */
    public void init(){
        lblPosGeo=new Text("POSIZIONE GEOGRAFICA");
        lblPosGeo.setStyleAttribute("color", "red");
        lblPosGeo.setStyleAttribute("text-align", "center");
        lblPosGeo.setStyleAttribute("font-weight", "bold");
        lblPosGeo.setStyleAttribute("font-size", "18px");
        lblDescription=new Text("DESCRIZIONE");
        lblDescription.setStyleAttribute("color", "red");
        lblDescription.setStyleAttribute("text-align", "center");
        lblDescription.setStyleAttribute("font-weight", "bold");
        lblDescription.setStyleAttribute("font-size", "18px");
        lblActivity=new Text("ATTIVITA' SVOLTE");
        lblActivity.setStyleAttribute("color", "red");
        lblActivity.setStyleAttribute("text-align", "center");
        lblActivity.setStyleAttribute("font-weight", "bold");
        lblActivity.setStyleAttribute("font-size", "18px");
        lblTrail=new Text("SENTIERI PERCORRIBILI");
        lblTrail.setStyleAttribute("color", "red");
        lblTrail.setStyleAttribute("text-align", "center");
        lblTrail.setStyleAttribute("font-weight", "bold");
        lblTrail.setStyleAttribute("font-size", "18px");
        txtName=new Text();
        txtName.setStyleAttribute("color", "red");
        txtName.setStyleAttribute("text-align", "center");
        txtName.setStyleAttribute("font-weight", "bold");
        txtName.setStyleAttribute("font-size", "35px");
        txtName.setStyleAttribute("font-family", "Rock Salt");
        txtTitle=new Text();
        txtTitle.setStyleAttribute("color", "red");
        txtTitle.setStyleAttribute("text-align", "center");
        txtTitle.setStyleAttribute("font-weight", "bold");
        txtTitle.setStyleAttribute("font-size", "28px");
//        txtName.setStyleAttribute("font-family", "Courier New");
        txtPosition=new Text();
        txtPosition.setStyleAttribute("text-align", "center");
        txtDescription=new Text();
        txtDescription.setStyleAttribute("text-align", "center");
        image=new HTML();
        tableAct=new FlexTable();
        tableTr=new FlexTable();
    }
    
    /**
     * Permettere di ottenere tutti dati nel datatstore relativo al rifugio
     * @param id id del rifugio da visualizzare
     */
    private void getInfoHut(String id){
        hutService.getHutActivity(id, new AsyncCallback<List<Activity>>() {
            
            public void onSuccess(List<Activity> result) {
                activities=new ArrayList<Activity>();
                for (int i=0;i<result.size();i++) {
                    activities.add(result.get(i));
                    Text act=new Text(result.get(i).getName());
                    act.setStyleAttribute("text-decoration", "underline");
                    act.setStyleAttribute("color", "blue");
                    act.addListener(Events.OnClick, new Listener<BaseEvent>() {
                        
                        public void handleEvent(BaseEvent be) {
                            BaseLayout.changeContent(new ActivityLayout(((Text)be.getSource()).getText(),true));
                        }
                    });
                    tableAct.setWidget(i, 0, act);
                }
                layout();
                
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        hutService.getHutTrail(id, new AsyncCallback<List<Trail>>() {
            
            public void onSuccess(List<Trail> result) {
                trails=new ArrayList<Trail>();
                for (int i=0;i<result.size();i++) {
                    trails.add(result.get(i));
                    Text tr=new Text(result.get(i).getName());
                    tr.setStyleAttribute("text-decoration", "underline");
                    tr.setStyleAttribute("color", "blue");
                    tr.addListener(Events.OnClick, new Listener<BaseEvent>() {
                        
                        public void handleEvent(BaseEvent be) {
                            BaseLayout.changeContent(new TrailLayout(((Text)be.getSource()).getText(),true));
                        }
                    });
                    tableTr.setWidget(i, 0, tr);
                }
                layout();
                
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        hutService.getHutName(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtName.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        hutService.getHutTitle(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtTitle.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        hutService.getHutDDescription(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtDescription.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        hutService.getHutImg(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                image.setHTML("<center><img src='images/rifugi/"+result+"' width='750px'/></center>");
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        hutService.getHutPosition(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtPosition.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
    }
    
    private void addComponent(){
        add(txtName);
        add(txtTitle);
        add(image);
        add(lblPosGeo);
        add(txtPosition);
        add(lblDescription);
        add(txtDescription);
        add(lblActivity);
        add(tableAct);
        add(lblTrail);
        add(tableTr);
    }
}
